﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DOTNETGROUP.Data;
using DOTNETGROUP.Entities;

public partial class Controls_ControlDichVuContent : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            HienThi_Domain();
            HienThi_Hosting();
            HienThi_ChamsocWeb();
            HienThi_Seo();
        }
    }

    protected void HienThi_Domain()
    {
        int toTalRows = 0;
        TList<LoaiDichVu> tlListLoaiDichVu = DataRepository.LoaiDichVuProvider.GetPaged("TenLoaiDichVu like N'%" + Common.ldvTenMienWeb + "%'", "ID DESC", 0, 0, out toTalRows);
        if (tlListLoaiDichVu.Count > 0)
        {
            lbTomtatTenmien.Text = tlListLoaiDichVu[0].TomTat;
            lbNoidung.Text = Server.HtmlDecode(tlListLoaiDichVu[0].NoiDung);
            imgTenmien.ImageUrl = "~/" + "Upload/AnhDaiDien/" + (tlListLoaiDichVu[0].AnhDaiDien.Trim() == "0" ? "no_image.jpg" : tlListLoaiDichVu[0].AnhDaiDien);
        }
    }

    protected void HienThi_Hosting()
    {
        int toTalRows = 0;
        TList<LoaiDichVu> tlListLoaiDichVu = DataRepository.LoaiDichVuProvider.GetPaged("TenLoaiDichVu like N'%" + Common.ldvLuutruWeb + "%'", "ID DESC", 0, 0, out toTalRows);
        if (tlListLoaiDichVu.Count > 0)
        {
            lblTomtatHosting.Text = tlListLoaiDichVu[0].TomTat;
            lblNoidungHosting.Text = Server.HtmlDecode(tlListLoaiDichVu[0].NoiDung);
            imgHosting.ImageUrl = "~/" + "Upload/AnhDaiDien/" + (tlListLoaiDichVu[0].AnhDaiDien.Trim() == "0" ? "no_image.jpg" : tlListLoaiDichVu[0].AnhDaiDien);
        }
    }

    protected void HienThi_ChamsocWeb()
    {
        int toTalRows = 0;
        TList<LoaiDichVu> tlListLoaiDichVu = DataRepository.LoaiDichVuProvider.GetPaged("TenLoaiDichVu like N'%" + Common.ldvChamsocWeb + "%'", "ID DESC", 0, 0, out toTalRows);
        if (tlListLoaiDichVu.Count > 0)
        {
            lblTomtatCsws.Text = tlListLoaiDichVu[0].TomTat;
            lblNoidungCsws.Text = Server.HtmlDecode(tlListLoaiDichVu[0].NoiDung);
            imgCsws.ImageUrl = "~/" + "Upload/AnhDaiDien/" + (tlListLoaiDichVu[0].AnhDaiDien.Trim() == "0" ? "no_image.jpg" : tlListLoaiDichVu[0].AnhDaiDien);
        }
    }

    protected void HienThi_Seo()
    {
        int toTalRows = 0;
        TList<LoaiDichVu> tlListLoaiDichVu = DataRepository.LoaiDichVuProvider.GetPaged("TenLoaiDichVu like N'%" + Common.ldvSeoWeb + "%'", "ID DESC", 0, 0, out toTalRows);
        if (tlListLoaiDichVu.Count > 0)
        {
            lblTomtatSeo.Text = tlListLoaiDichVu[0].TomTat;
            lblNoidungSeo.Text = Server.HtmlDecode(tlListLoaiDichVu[0].NoiDung);
            imgSeoWeb.ImageUrl = "~/" + "Upload/AnhDaiDien/" + (tlListLoaiDichVu[0].AnhDaiDien.Trim() == "0" ? "no_image.jpg" : tlListLoaiDichVu[0].AnhDaiDien);
        }
    }

    /// <summary>
    /// Xu ly su kien kiem tra ten mien
    ///  + Sau do hien thi cac de xuat cho nguoi dung dang ky ten mien
    ///  + Cac ten mien nhu .com, .vn, .biz
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btCheck_Click(object sender, EventArgs e)
    {
        //// Thuc hien goi ham kiem tra ten mien
        ////img_loading.Visible = true;
        //panelBaogia.Visible = true;        
        //List<String> kqKiemtra = KiemtraTenmien(tbTenmien.Text.Trim());

        //// Sau do la hien thi du lieu ra DataList
        //dtlDstenmien.DataSource = kqKiemtra;
        //dtlDstenmien.DataBind();


        ////////////// Kiem tra chuoi ton tai/////////////////
        string s = "";
        if (TabDomain.ActiveTabIndex == 0)
        {
            foreach (ListItem li in chkPhoBien.Items)
            {
                if (li.Selected == true)
                {
                    s += "|" + li.Text;
                    LoadDomain(tbTenmien.Text, s.Substring(1));
                }
            }
        }
        else if (TabDomain.ActiveTabIndex == 1)
        {
            foreach (ListItem li in chkVietNam.Items)
            {
                if (li.Selected == true)
                {
                    s += "|" + li.Text;
                    LoadDomain(tbTenmien.Text, s.Substring(1));
                }
            }
        }
        else if (TabDomain.ActiveTabIndex == 2)
        {
            foreach (ListItem li in chkQuocTe.Items)
            {
                if (li.Selected == true)
                {
                    s += "|" + li.Text;
                    LoadDomain(tbTenmien.Text, s.Substring(1));
                }
            }
        }
        //////
    }

    /// <summary>
    /// Phuong thuc kiem tra ten mien va tra ve danh sach cac ten mien co san ma co the dang ky duoc
    /// </summary>
    /// <param name="chuoi"></param>
    /// <returns></returns>
    protected List<String> KiemtraTenmien(string chuoi)
    {
        string elementKq = "";
        List<String> arrListKQ = new List<string>();
        string[] tenMien = { ".com", ".vn", ".com.vn", ".net", ".info", ".biz", ".net.vn", ".org", ".org.vn", ".it" };

        foreach (var s in tenMien)
        {
            try
            {
                var isCheck = Dns.GetHostAddresses("www." + chuoi + s);

                if (isCheck.Count() > 0)
                {
                    elementKq += "[1:" + chuoi + s + ":" + isCheck[0] + ":null:null]";
                }
                else
                {
                    elementKq += "[0:" + chuoi + s + ":" + isCheck[0] + ":null:null]";
                }
            }
            catch (Exception)
            {
                // Khong ton tai ten mien
                elementKq += "[0:" + chuoi + s + ":null:null:null]";                
            }
            
            // Them element do vao mang arrKetQua
            arrListKQ.Add(elementKq);
        }


        return arrListKQ;
    }

    /// <summary>
    /// Tach chuoi va lay phan tu theo thu tu yeu cau
    /// </summary>
    /// <param name="chuoi"></param>
    /// <param name="pt"></param>
    /// <returns></returns>
    public string tachChuoi(string chuoi, int pt)
    {
        string[] arrChuoi = chuoi.Split(':');
        if (arrChuoi.Count() > 0)
        {
            return arrChuoi[pt];
        }
        return arrChuoi[pt];
    }

    /// <summary>
    /// Phuong thuc LoadDomain
    /// </summary>
    /// <param name="Domain"></param>
    /// <param name="Ext"></param>
    protected void LoadDomain(string Domain, string Ext)
    {
        int iCount = 0;
        string strCheck = "";
        string strToCheck = Domain;
        string strDomain = Ext;
        string[] strSplitEx = strDomain.Split(new char[] { '|' });
        StringBuilder strOut = new StringBuilder();

        strOut.Append("<b>KẾT QUẢ</b>");
        strOut.Append("<table cellpadding=\"2\" cellspacing=\"2\" border=\"0\">");

        foreach (string strExt in strSplitEx)
        {
            strCheck = strToCheck + strExt;

            strOut.Append("     <tr>");
            strOut.Append("         <td>");
            strOut.Append("             <span id=\"domain_").Append(iCount).Append("\">");
            strOut.Append(strCheck);
            strOut.Append("             </span>");
            strOut.Append("         </td>");
            strOut.Append("         <td>");
            strOut.Append("             <span id=\"check_").Append(iCount).Append("\"></span><br />");
            strOut.Append("             <script language=\"javascript\" type=\"text/javascript\">CheckDomain('").Append(strToCheck).Append("','").Append(strExt).Append("','check_").Append(iCount).Append("');</script>");
            strOut.Append("         </td>");
            strOut.Append("     </tr>");

            iCount++;
        }
        strOut.Append("</table>");

        divResult.InnerHtml = strOut.ToString();
    }
}